package com.ruoyi.web.controller.team;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.team.domain.Contract;
import com.ruoyi.team.service.IContractService;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 合同信息Controller
 *
 * @author shier
 * @date 2022-11-27
 */
@RestController
@RequestMapping("/team/contract")
public class ContractController extends BaseController {
    @Autowired
    private IContractService contractService;

    /** 查询合同信息列表 */
    @PreAuthorize("@ss.hasPermi('team:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract) {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /** 获取合同信息详细信息 */
    @PreAuthorize("@ss.hasPermi('team:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(contractService.getById(id));
    }

    /** 新增合同信息 */
    @PreAuthorize("@ss.hasPermi('team:contract:add')")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract) {
        return toAjax(contractService.save(contract));
    }

    /** 修改合同信息 */
    @PreAuthorize("@ss.hasPermi('team:contract:edit')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Contract contract) {
        return toAjax(contractService.updateById(contract));
    }

    /** 删除合同信息 */
    @PreAuthorize("@ss.hasPermi('team:contract:remove')")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids) {
        return toAjax(contractService.removeById(ids));
    }

    /** 修改审核状态 */
    //@PreAuthorize("@ss.hasPermi('team:contract:audit')")
    @PostMapping("/audit")
    public AjaxResult changeAuditStatus(Long id, Boolean auditStatus) {
        return toAjax(contractService.updateAuditStatusById(id, auditStatus));
    }

    /** 盖章 */
    //@PreAuthorize("@ss.hasPermi('team:contract:affixSeal')")
    @PostMapping("/affixSeal")
    public AjaxResult changeAffixSealState(Long id) {
        return toAjax(contractService.updateAffixSealById(id));
    }

    /** 下载文件 */
    //@PreAuthorize("@ss.hasPermi('team:contract:download')")
    @PostMapping("/download")
    public AjaxResult downloadFileByMinioUrl(Long id, HttpServletRequest request, HttpServletResponse response) {
        /*contractService.downloadFile(id, request, response);*/
        Contract contract = contractService.getById(id);

        String url = contract.getAppendix();

        if (url == null) {
            throw new SecurityException("当前合同暂无文件, 不能进行下载");
        }

        /*FileDownloadUtils.downloadFileURL(contract.getAppendix(), request, response);*/

        return AjaxResult.success("success", url);
    }
}
